//@auther foolbread
//@time 2016-03-09
//@file ApnsHttp2/server/server.go
package server

import (
	"ApnsHttp2/client"
	"ApnsHttp2/config"
	"github.com/foolbread/fbcommon/golog"
	"net"
)

func InitApnsServer() {
	golog.Info("initing ApnsHttp2 server...")
	g_server = newApnsServer()
	go g_server.startServer()
}

var g_server *apnsServer

type apnsServer struct {
	addr string
}

func newApnsServer() *apnsServer {
	r := new(apnsServer)
	r.addr = config.GetConfigInstance().GetListenAddr()
	return r
}

func (s *apnsServer) startServer() {
	golog.Info("listen addr:", s.addr)
	l, err := net.Listen("tcp", s.addr)
	if err != nil {
		golog.Critical(err)
	}

	for {
		c, err := l.Accept()
		if err != nil {
			golog.Critical(err)
		}

		cli := client.NewClient(c)

		golog.Info("new connect client:", c.RemoteAddr().String())

		go cli.Run()
	}
}
